/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * All Rights Researved.
 *
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 *
 * @author: jin Chen
 * @date: Jun 30, 2003$
 * @version: 1.0
 */
package edu.psu.geovista.app.pcp.plot.control.tools;

import edu.psu.geovista.app.pcp.plot.gui.BasicPcpUI;
import edu.psu.geovista.app.pcp.plot.layer.AxisLayer;
import edu.psu.geovista.app.pcp.plot.layer.BasicLayer;
import edu.psu.geovista.app.pcp.plot.layer.ConfigInfo;
import edu.psu.geovista.app.pcp.plot.layer.DataLayer;
import edu.psu.geovista.app.pcp.plot.services.CommonService;
import edu.psu.geovista.app.plot.axis.AxisList;
import edu.psu.geovista.app.plot.axis.YAxis;
import edu.psu.geovista.app.plot.layer.Layer;

import java.util.Hashtable;


public class BasicTool {
    public static final boolean DEBUG = false;
    public static final String AxisScaleT="AxisScaleTool";
    public static final String AxisShowRangeT="AxisShowRangeTool";
    public static final String AxisOrderT="AxisOrderTool";
    public static final String AxisMoveT="AxisMoveCmd";
    public static final String AxisPlaceT="AxisPlCmd";
    public static final String PlotT="PlotTool";
    public static final String DataSelectT="DataSelectTool";
    public static final String DataSetColorT="DataSetColorTool";
    public static final String AnmT="AnimationCmd";
    public static final String DMouseT="DMCmd";//Default mouse cmd
    public static final String ScanBoxTool="frmScnT";

     protected YAxis activeAxis;//active axis
     //protected AxisList axisList;  //YAxis list
     protected BasicPcpUI ui;
     protected boolean toolOn=false;//ture: tool is turned on



    public BasicTool(BasicPcpUI ui) {
        this.ui = ui;

        /*AxisLayer axisLayer =(AxisLayer)ui.getLayer(Layer.AXIS_LAYER ) ;
        if(axisLayer!=null)
            this.axisList =axisLayer.getAxisList() ;*/
    }

    public void repaintPlot() {
        //Thread.dumpStack() ;
        ui.getPlotCanvas().repaint();
        /*DfPcp plot = (DfPcp) ui.getPlot();
        plot.repaint();*/
    }

    public void setActiveAxis(YAxis activeAxis) {
       /* if(activeAxis!=null)
            System.out.println("******** set active axis:"+this.getClass().getName()+" set "+activeAxis.getName());
        else
           // System.out.println("");*/

        this.activeAxis = activeAxis;
    }
    public boolean isToolOn() {
        return toolOn;
    }

    public void setToolOn(boolean toolOn) {
        this.toolOn = toolOn;
    }

    public BasicLayer getLayer(String name){
        return (BasicLayer)ui.getLayers().get(name);
    }
    public Object[] getLayerByType(int type){

        return this.ui.getLayerByType(Layer.TYPE_DATA );
    }
    public Hashtable getLayers() {
         return ui.getLayers() ;
    }

    public BasicTool getCmd(String name){
        return this.ui.getToolsMgr().getCmd(name);
    }

    public CommonService getService(){
        return ui.getService() ;
    }
    public ConfigInfo getConfigInfo(String layerName){
        Layer layer=this.getLayer(layerName ) ;
        if(layer!=null){
          return layer.getConfigInfo() ;
        }
        else
            return null;
    }

    public AxisList getAxisList() {
        AxisLayer axisLayer =(AxisLayer)ui.getLayer(Layer.AXIS_LAYER ) ;
        if(axisLayer!=null)
            return axisLayer.getAxisList() ;
        else
            return null;
    }
    /**
     *
     * @return  int represent whether the plot is PCP or Time Series
     */
    public int getPlotMode(){
        return getUI().getPlot().getMode() ;
    }
    public edu.psu.geovista.data.model.AppDataModel getDataModel(){
        return getUI().getPlot().getDataModel();
    }
    /*public static int[] convertToArray(HashSet set){
        long start,stop; //put is at start of method
                if(DEBUG){//put is at start of method
                    start=Calendar.getInstance().getTime().getTime() ;
                }
        int[] intA;
        if (set.size() <=0){
            intA=null;
        }
        else{
           intA=new int[set.size() ];
            Object[] tmp=set.toArray() ;
            for (int i=0;i<intA.length ;i++){
                intA[i]=((Integer)tmp[i]).intValue() ;
            }
        }

                if(DEBUG){  //put is at end of method
                    stop=Calendar.getInstance().getTime().getTime() ;
                    System.out.println("convertToArray() take "+(stop-start)*0.001f+" to finish" );
                }

        return intA;
    }*/
    //convenient method to repaint layers of data layer type
    public  void repaintDataLayers(){
         Object[] dataLys=this.getLayerByType(Layer.TYPE_DATA );
                 for (int i=0;i<dataLys.length ;i++){
                     DataLayer dataLayer=(DataLayer)dataLys[i];//(DataLayer)this.ui.getLayer(Layer.AllData_LAYER );
                     dataLayer.repaintLayer();
                 }
                 this.repaintPlot() ;

     }

     /**
     * reset a tool to initial state. Since a tool has some state, need clear it before a new session.
     * Need overrided by subclass
     */
    public void reset(){

    }
    /**
     *  
     *  
     * overrided by subclass
     */
    public void dispose(){

    }

    public BasicPcpUI getUI() {
        return ui;
    }

}
